home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1994 November / Cd Ware (Nro. 2) - Epimundo.iso / DOS / PG / NPOS52.ZIP / NTXPOS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-03  |  5.1 KB  |  224 lines

  1. //.............................................................................
  2. //
  3. //   Program Name: NTXPOS.C          Copyright: RCM Software Pty. Ltd.                                   
  4. //   Date Created: 27/04/91           Language: Microsoft C 5.1                                         
  5. //   Time Created: 17:49:22             Author: Graham D. McKechnie                       
  6. //
  7. //   Modified 9/25/92 7:00 pm
  8. //   by Brian Marasca
  9. //   Use index handle rather than file name.
  10. //   IndexOrd() is now passed as parameter 1.
  11. // 
  12. //   Returns index record number
  13. //   eg. nNtxPos := NtxPos( IndexOrd(), RECNO() )
  14. //.............................................................................
  15.         
  16. #include "extend.api"
  17. #include "fm.api"
  18. #include "stdio.h"
  19.  
  20. #define open  _topen                // Use Clipper's internals
  21. #define close _tclose               // Don't have to link LLIBCA
  22. #define lseek _tlseek
  23. #define read  _tread
  24.  
  25. extern int  _topen( char*, int);
  26. extern int  _tclose( int );
  27. extern long _tlseek( int, long, int);
  28. extern int  _tread( int, char*, int);
  29.  
  30. #define BUFF_SIZE   1024
  31. #define ERROR       -1
  32. #define MAX_KEY     256
  33. #define O_RDONLY    0x0000  // open for reading only 
  34. #define O_BINARY    0x8000  // file mode is binary (untranslated) 
  35.  
  36.  
  37. // Index structures 
  38. typedef struct
  39. {
  40.    unsigned  uSign;
  41.    unsigned  uVersion;
  42.    long      lRoot;
  43.    long      lNextPage;
  44.    unsigned  uItemSize;
  45.    unsigned  uKeySize;
  46.    unsigned  uKeyDec;
  47.    unsigned  uMaxItem;
  48.    unsigned  uHalfPage;
  49.    char      cKeyExpr[MAX_KEY];
  50.    Boolean   bUnique;
  51. } NTXHEADER;
  52.  
  53.  
  54. typedef struct
  55. {
  56.    long lPage;
  57.    long lRecNo;
  58.    char cKey;
  59. } ITEM;
  60.  
  61.  
  62. typedef struct
  63. {
  64.    unsigned uCount;
  65.    unsigned uRef;
  66. } BUFFER;
  67.  
  68. int getNtxHandle( int n ) ;
  69. CLIPPER ntxpos( void ) ;
  70. void dumpPage( long lPageOffSet, long lRecNo ) ;
  71.  
  72. static Boolean bError = FALSE;
  73. static Boolean bFound = FALSE;
  74.  
  75. static long lRetVal;            // Return value
  76. static long lNtxPos;            // Index position
  77. static int  nNtxHandle;         // Index file handle
  78.  
  79.  
  80. CLIPPER ntxpos()
  81. {
  82.    long lRecNo;
  83.    int nNtxOrd;
  84.    long nNtxPtr;
  85.  
  86.    NTXHEADER NtxHeader;
  87.  
  88.    // Check the parameters passed from Clipper
  89.    if ( PCOUNT != 2 )
  90.    {
  91.       _retni(-1);
  92.       return;
  93.    }
  94.  
  95.    if ( ! (ISNUM(1) && ISNUM(2) ) )
  96.    {
  97.       _retni(-1);
  98.       return;
  99.    }
  100.  
  101.    nNtxOrd  = _parni(1);    // indexord() to go by
  102.    lRecNo   = _parnl(2);    // RECNO() we are looking for
  103.  
  104.    lRetVal  = 0L;           // Set these each time
  105.    lNtxPos  = 0L;
  106.    bFound   = FALSE;
  107.  
  108.    if ( ( nNtxHandle = getNtxHandle ( nNtxOrd ) ) != -1 )
  109.    {
  110.         /* save the ntx file position and reset it to the top */
  111.         nNtxPtr = _tlseek( nNtxHandle, 0L, SEEK_CUR );
  112.         _tlseek( nNtxHandle, 0L, SEEK_SET );
  113.  
  114.         /* read the header */
  115.         if ( read ( nNtxHandle, (char *) &NtxHeader, sizeof(NtxHeader) )
  116.                        != sizeof(NtxHeader) )
  117.         goto ERROR_EXIT; 
  118.  
  119.         /* start the traversal from root */
  120.         dumpPage( NtxHeader.lRoot, lRecNo );
  121.  
  122.         if (bError)
  123.         {
  124.              ERROR_EXIT:
  125.              bError = TRUE;
  126.         }
  127.  
  128.         /* restore the ntx file position */
  129.         _tlseek( nNtxHandle, nNtxPtr, SEEK_SET );
  130.    }
  131.  
  132.    _retnl( lRetVal );
  133.  
  134. }
  135.  
  136.  
  137. void dumpPage(long lPageOffSet, long lRecNo)
  138. {
  139.    char     *cPage;
  140.    ITEM     *item;
  141.    BUFFER   *buffer;
  142.    unsigned i;
  143.    unsigned *uItemRef;
  144.  
  145.    /* allocate page */
  146.    cPage = _xalloc(BUFF_SIZE);
  147.  
  148.    /* move to this position in the file */
  149.    if( lseek( nNtxHandle, lPageOffSet, 0 ) != (long)lPageOffSet )
  150.       goto DUMP_EXIT;
  151.  
  152.    /* read the page */
  153.    if( read( nNtxHandle, cPage, BUFF_SIZE) != BUFF_SIZE )
  154.       goto DUMP_EXIT;
  155.  
  156.  
  157.    buffer   = (BUFFER *) cPage;
  158.    uItemRef = &buffer -> uRef;
  159.  
  160.  
  161.    for (i = 0; i < buffer -> uCount; i++)
  162.    {
  163.       item = (ITEM *) &cPage[ uItemRef[ i ] ];
  164.  
  165.       if ( ! bFound )
  166.       {
  167.          if ( item -> lPage )
  168.             dumpPage( item -> lPage, lRecNo );
  169.  
  170.          if (bFound)
  171.          {
  172.             _xfree( cPage );
  173.             return;
  174.          }  
  175.          lNtxPos++;
  176.       }
  177.  
  178.       if ( item->lRecNo == lRecNo )
  179.       {
  180.          lRetVal = lNtxPos;
  181.          bFound = TRUE;
  182.          break;
  183.       }
  184.    }
  185.  
  186.    /* handle extra right pointer */
  187.    item = (ITEM *) &cPage[ uItemRef[ buffer -> uCount ] ];    
  188.    if (item -> lPage)
  189.       dumpPage(item -> lPage, lRecNo );
  190.  
  191.    _xfree( cPage );
  192.  
  193.    if ( bError )
  194.    {
  195.       DUMP_EXIT:
  196.       _xfree( cPage );
  197.       bError = TRUE;
  198.    }
  199. }
  200.  
  201. /**************************************************************************
  202.    FUNCTION: getNtxHandle ( n )
  203.    Returns the index handle for the current workarea, or -1 if error.
  204.    Brian Marasca 9/25/92
  205.  
  206.    Modified for 5.2.
  207. **************************************************************************/
  208.  
  209. int getNtxHandle( n )
  210. int n ;
  211. {
  212.      extern char **_workareas ;
  213.      int **hp, han = -1 ;
  214.  
  215.      if(n > 0 && n <= 15)
  216.      {
  217.           hp = (int **) ((*_workareas) + 0xA0 + ((n-1) * sizeof (char *))) ;
  218.           han = *hp ? **hp : -1 ;
  219.      }
  220.  
  221.      return han ;
  222. }
  223.  
  224.